// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-05 22:12
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.SlideWindows;

import java.util.Arrays;

public class FindMaxAverage {
    /**
     * 寻找子数组的最大平均数：
     * 子数组的最大平均数的取值一定在一个连续的区间
     */
    public double findMaxAverage(int[] nums, int k) {
        int n = nums.length;
        int[] sum = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            sum[i] = nums[i - 1] + sum[i - 1];
        }
        double ans = (sum[k] - sum[0]) / (double) k;
        for (int i = k + 1; i <= n; i++) {
            ans = Math.max(ans, (sum[i] - sum[i - k]) / (double) k);
        }
        return ans;
    }
}
